<?php
header('Content-Type: application/json');
$url = $_POST['url'];
$private_key = $_POST['private_key'];
//访问$url
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'url' => $url
]);
$origin = $_SERVER['HTTP_ORIGIN']?? '';
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Origin: '. $origin,
    'Accept: application/json',
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if($data['code'] == 200) {
    $encrypt_data = $data['data'];
    openssl_private_decrypt(base64_decode($encrypt_data), $decrypted, $private_key);
    $decrypted_data = json_decode($decrypted, true);
    if(!empty($decrypted_data)){
        $data = [
            'success' => true,
            'code' => 200,
            'msg' => $data['msg'],
            'data' => $decrypted_data,
        ];
    } else {
        $data = [
           'success' => false,
           'code' => 400,
           'msg' => '解密失败，请检查私钥是否正确，是否和API接入管理对应的公钥配对',
        ];
    }
} else {
    $data = [
        'success' => false,
        'code' => 400,
        'msg' => $data['msg'],
    ];
}

echo json_encode($data);
exit;